﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using System.Windows.Forms;

namespace TA.Turbo.Design
{
	public class ValidationGroupEditor : UITypeEditor
	{
		public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
		{
			return UITypeEditorEditStyle.DropDown;
		}
		public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
		{
			this._edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
			System.Web.UI.Control startingPoint = context.Instance as System.Web.UI.Control;
			if (this._edSvc != null && startingPoint != null)
			{
				ListBox lb = new ListBox();
				lb.SelectedIndexChanged += new EventHandler(lb_SelectedIndexChanged);
				lb.Items.AddRange(this.GetLister().GetValidationGroupList(startingPoint));

				this._edSvc.DropDownControl(lb);
				if (!string.IsNullOrEmpty(lb.Text))
					value = lb.Text;
				lb.Dispose();
				return value;
			}
			return base.EditValue(context, provider, value);
		}
		void lb_SelectedIndexChanged(object sender, EventArgs e)
		{
			this._edSvc.CloseDropDown();
		}
		IWindowsFormsEditorService _edSvc;
		protected virtual BaseValidationGroupLister GetLister()
		{
			return new ValidationGroupLister();
		}
	}
}
